<!--
  - Copyright (c) 2019.  武汉中科图灵科技有限公司
  - Date :  2019/1/5
  - Author ：F12 Console=> atob("MzMxNDkxODAwQHFxLmNvbQ==")
  - Version ：1.6.2
  -->

<template>
    <div class="app-container calendar-list-container no-padding">
        <el-row>
            <div class="xf-tabletop-handles">
                <div class="xf-tabhd-left">
                    <!--<el-button class="filter-item" @click="handleCreate" type="primary">添加传感器类型</el-button>-->
                </div>
                <div class="xf-tabhd-right">
                    厂商
                    <el-select v-model="listQuery.manufacturer" placeholder="请选择" class="xf-query-select" size="mini">
                        <el-option v-for="item in queryKeysComp" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                    系列
                    <el-select v-model="listQuery.equipmentType" placeholder="请选择" class="xf-query-select" size="mini">
                        <el-option v-for="item in queryKeysSeri" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                    型号
                    <el-input v-model="listQuery.model" placeholder="请输入型号" class="xf-query-input" size="mini"></el-input>
                    <el-button @click="tableQuery" type="primary" size="mini">查询</el-button>
                </div>
            </div>
        </el-row>
        <el-table key='tableKey' :data="list" v-loading.body="listLoading" style="width: 100%">
            <el-table-column label="类型编号" width="100">
                <template slot-scope="scope">
                    <span>{{scope.row.sensorType.id}}</span>
                </template>
            </el-table-column>
          <el-table-column label="所属系统">
            <template slot-scope="scope">
              <span>{{scope.row.sensorType.channelName}}</span>
            </template>
          </el-table-column>
            <el-table-column label="厂商">
                <template slot-scope="scope">
                    <span>{{scope.row.sensorType.sensorManufacturer}}</span>
                </template>
            </el-table-column>
            <el-table-column label="系列">
                <template slot-scope="scope">
                    <span>{{scope.row.sensorType.equipmentType}}</span>
                </template>
            </el-table-column>
            <el-table-column label="型号">
                <template slot-scope="scope">
                    <span>{{scope.row.sensorType.model}}</span>
                </template>
            </el-table-column>
            <!--<el-table-column label="数据采集周期">-->
                <!--<template slot-scope="scope">-->
                    <!--<span>{{scope.row.dataAcquisitionCycle}}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column label="采集延迟时间">-->
                <!--<template slot-scope="scope">-->
                    <!--<span>{{scope.row.acquisitionDelayTime}}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column label="维保周期">-->
                <!--<template slot-scope="scope">-->
                    <!--<span>{{scope.row.maintenanceCycle}}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column label="操作" width="150">-->
                <!--<template slot-scope="scope">-->
                    <!--<el-button size="small" plain @click="handleUpdate(scope.row)">-->
                        <!--<icon-svg icon-class="bianji" style="fill:#2683f5" />-->
                    <!--</el-button>-->
                    <!--<el-button size="small" plain @click="handleDelete(scope.row)">-->
                        <!--<icon-svg icon-class="shanchu" style="fill:#2683f5" />-->
                    <!--</el-button>-->
                <!--</template>-->
            <!--</el-table-column>-->
        </el-table>
        <div v-show="!listLoading" class="pagination-container">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[15,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
        </div>
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :fullscreen='true' :show-close='false' custom-class="fulldialog">
            <!-- 自定义关闭按钮 -->
            <span class="custom-dialog-btn" @click="cancel('form')">
                <i class="el-icon-close"></i>
            </span>
            <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position='top'>
                <el-form-item label="厂商" prop="manufacturer">
                  <el-select v-model="form.manufacturer" placeholder="请选择厂商" style="width:100%" :disabled="dialogStatus=='update'">
                        <el-option v-for="item in selectManufacturer" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                    <!-- <el-input v-model="form.manufacturer" placeholder="请输入厂商" :disabled="dialogStatus=='update'"></el-input> -->
                </el-form-item>
                <el-form-item label="系列" prop="equipmentType">
                    <el-select v-model="form.equipmentType" placeholder="请选择系列" style="width:100%" :disabled="dialogStatus=='update'">
                        <el-option v-for="item in selectSeri" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="型号" prop="model">
                    <el-input v-model.trim="form.model" placeholder="请输入型号" :disabled="dialogStatus=='update'"></el-input>
                </el-form-item>
                <el-form-item label="数据采集周期" prop="dataAcquisitionCycleValue">
                    <el-input placeholder="请输入内容" v-model="form.dataAcquisitionCycleValue" class="input-with-select" maxlength=9>
                        <el-select v-model="selectDatacy" slot="append" placeholder="请选择" class="tmselect">
                            <el-option label="秒" value="0"></el-option>
                            <el-option label="分钟" value="1"></el-option>
                            <el-option label="小时" value="2"></el-option>
                            <el-option label="天" value="3"></el-option>
                        </el-select>
                    </el-input>
                </el-form-item>
                <el-form-item label="采集延时时间" prop="acquisitionDelayTimeValue">
                    <el-input placeholder="请输入内容" v-model="form.acquisitionDelayTimeValue" class="input-with-select" maxlength=9>
                        <el-select v-model="selectDely" slot="append" placeholder="请选择" class="tmselect">
                        <el-option label="秒" value="0"></el-option>
                        <el-option label="分钟" value="1"></el-option>
                        <el-option label="小时" value="2"></el-option>
                        <el-option label="天" value="3"></el-option>
                        </el-select>
                    </el-input>
                </el-form-item>
                <el-form-item label="维保周期" prop="maintenanceCycleValue">
                    <el-input placeholder="请输入内容" v-model="form.maintenanceCycleValue" class="input-with-select" maxlength=9>
                        <el-select v-model="selectFix" slot="append" placeholder="请选择" class="tmselect">
                        <el-option label="小时" value="0"></el-option>
                        <el-option label="天" value="1"></el-option>
                        <el-option label="年" value="2"></el-option>
                        </el-select>
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')" round size="medium">确 定</el-button>
                <el-button v-else type="primary" @click="update('form')" round size="medium">确 定</el-button>
                <el-button @click="cancel('form')" round size="medium">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog title="删除传感器类型" :visible.sync="dialogDeleteVisible" :show-close='false' custom-class="xf-jc-dialog" width="360px">
            <div class="xf-jc-deletect">
                <p v-if="deleteJumpObj>0">有{{deleteJumpObj}}个传感器绑定了此类型，删除前请先解绑传感器</p>
                <p v-else>确定删除此传感器类型吗？</p>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="danger" round size="medium" v-if="deleteJumpObj<1" @click="deleteD(currentSelectObj)">删除</el-button>
                <el-button round size="medium" v-if="deleteJumpObj<1" @click="dialogDeleteVisible = false">取消</el-button>
                <el-button type="primary" round size="medium" v-if="deleteJumpObj>0" @click="dialogDeleteVisible = false">知道了</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

import { validatInter } from "@/utils/validate";

import {
  page,
  pageSelected,
  deleteQuery,
  addObj,
  getObj,
  delObj,
  putObj
} from "@/api/device/deviceSensorType/index";

export default {
  name: "sensorType",
  data() {
       const validateInterg = (rule, value, callback) => {
      if (!validatInter(value)) {
        callback(new Error("请输入大于0的整数"));
      } else {
        callback();
      }
    };
    return {
      selectSeri:[],
      queryKeysComp: [],
      queryKeysSeri: [],
      selectDatacy:'1',
      selectDely:'1',
      selectFix:'1',
      form: {
        manufacturer:undefined,
        equipmentType:undefined,
        model:undefined,
        dataAcquisitionCycleValue:undefined,
        acquisitionDelayTimeValue:undefined,
        maintenanceCycleValue:undefined,
      },
      rules: {
        manufacturer: [
           {
            required: true,
            message: "请输入厂商",
            trigger: "blur"
          },
          {
            min: 1,
            max: 100,
            message: "最多允许输入100个字符",
            trigger: "blur"
          }
        ],
        equipmentType: [
           {
            required: true,
            message: "请输入系列",
            trigger: "blur"
          },
          {
            min: 1,
            max: 50,
            message: "最多允许输入50个字符",
            trigger: "blur"
          }
        ],
        model: [
           {
            required: true,
            message: "请输入型号",
            trigger: "blur"
          },
          {
            min: 1,
            max: 100,
            message: "最多允许输入100个字符",
            trigger: "blur"
          }
        ],
        dataAcquisitionCycleValue: [
          {
            required: true,
            trigger: "blur",
            validator: validateInterg
          }
        ],
        acquisitionDelayTimeValue: [
          {
            required: true,
            trigger: "blur",
            validator: validateInterg
          }
        ],
        maintenanceCycleValue: [
          {
            required: true,
            trigger: "blur",
            validator: validateInterg
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 15,
        manufacturer: "全部",
        equipmentType: "全部",
        model: undefined
      },
      dialogFormVisible: false,
      dialogDeleteVisible:false,
      currentSelectObj: {},
      deleteJumpObj: "",
      dialogStatus: "",
      textMap: {
        update: "编辑传感器类型",
        create: "添加传感器类型"
      },
      tableKey: 0,
      selectManufacturer:[]
    };
  },
  created() {
    this.getList();
    this.queryKeys();
  },
  methods: {
    tableQuery() {
      this.getList();
    },
    handleCreate() {
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    cancel(formName) {
      this.$refs[formName].resetFields();
      this.resetTemp();
      this.dialogFormVisible = false;
    },
    handleUpdate(row) {
        getObj(row.id).then(response => {
        this.form = response.data;
        this.selectDatacy = this.form.dataAcquisitionCycleUnit;
        this.selectDely = this.form.acquisitionDelayTimeUnit;
        this.selectFix = this.form.maintenanceCycleUnit;
        this.dialogFormVisible = true;
        this.dialogStatus = "update";
      });
    },
    deleteD(row){
      this.dialogDeleteVisible = false;
      delObj(row.id).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.getList();
          this.queryKeys();
        });
    },
    handleDelete(row) {
      // 删除操作查询
      let self = this;
        deleteQuery(row.id).then(response => {
          if (response.status === 200) {
            self.currentSelectObj = row;
            self.dialogDeleteVisible = true;
            self.deleteJumpObj = response.data;
          }
        }).catch(function (error) {
              console.log(error);
            });
    },
    getList() {
      this.listLoading = true;
      page(this.listQuery).then(response => {
        this.list = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    queryKeys() {
      pageSelected().then(response => {
        this.queryKeysComp = response.data.manufacturer;
        this.queryKeysSeri = response.data.equipmentType;
        this.selectSeri = response.data.series;
        this.selectManufacturer = response.data.selectManufacturer;
      });
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          this.form.dataAcquisitionCycleUnit = this.selectDatacy;
          this.form.acquisitionDelayTimeUnit = this.selectDely;
          this.form.maintenanceCycleUnit = this.selectFix;

          putObj(this.form).then(() => {
            this.dialogFormVisible = false;
            this.resetTemp();
            this.$refs[formName].resetFields();
            this.getList();
            this.queryKeys();
            this.$notify({
              title: "成功",
              message: "修改成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.form.dataAcquisitionCycleUnit = this.selectDatacy;
          this.form.acquisitionDelayTimeUnit = this.selectDely;
          this.form.maintenanceCycleUnit = this.selectFix;
          addObj(this.form).then(() => {
            this.dialogFormVisible = false;

            this.$refs[formName].resetFields();
            this.resetTemp();

            this.getList();
            this.queryKeys();
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
          }).catch(function (error) {
              console.log(error);
            });
        } else {
          return false;
        }
      });
    },
    resetTemp() {
      this.form = {
        manufacturer:undefined,
        equipmentType:undefined,
        model:undefined,
        dataAcquisitionCycleValue:undefined,
        acquisitionDelayTimeValue:undefined,
        maintenanceCycleValue:undefined,
      };
      this.selectDatacy = '1';
      this.selectDely = '1';
      this.selectFix = '1';
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.el-table th {
  background: #f5f7fa;
}
.xf-query-input,
.xf-query-select {
  width: 200px;
  margin: 0 10px;
}
.fulldialog > div {
  width: 500px;
  margin: 0 auto;
}
.fulldialog .el-dialog__title {
  font-size: 32px;
}
.fulldialog {
  .el-dialog__header {
    margin-top: 20px;
  }
  .custom-dialog-btn {
    position: absolute;
    font-size: 30px;
    left: 40px;
    top: 20px;
    color: #fff;
    &:hover {
      cursor: pointer;
    }
  }
  .dialog-footer {
    text-align: left;
  }
//   带下拉选择的输入
.tmselect .el-input {
    width: 80px;
  }
  .input-with-select .el-input-group__append {
    background-color: #fff;
  }
}
</style>
